
/*
 * Copyright (C) 2014 Archie L. Cobbs. All rights reserved.
 *
 * $Id: CastFunction.java 663 2015-01-16 21:02:37Z archie.cobbs $
 */

package org.jsimpledb.util;

import com.google.common.base.Function;

/**
 * A {@link Function} that casts objects to some type.
 */
public class CastFunction<T> implements Function<Object, T> {

    protected final Class<T> type;

    /**
     * Constructor.
     *
     * @param type desired type
     * @throws IllegalArgumentException if {@code type} is null
     */
     public CastFunction(Class<T> type) {
        if (type == null)
            throw new IllegalArgumentException("null type");
        this.type = type;
    }

    @Override
    public T apply(Object obj) {
        if (obj == null)
            return null;
        try {
            return this.type.cast(obj);
        } catch (ClassCastException e) {
            throw this.handleFailure(obj, e);
        }
    }

    /**
     * Generate an exception to throw when a cast failure occurs.
     *
     * <p>
     * The implementation in {@link CastFunction} just throws {@code e}.
     * </p>
     *
     * @param obj object on which cast failed
     * @param e resulting exception
     * @return exception to throw
     */
    protected RuntimeException handleFailure(Object obj, ClassCastException e) {
        return e;
    }
}

